import { datas } from './fields.js'
let that
// 串口监听
export default {
	data() {
		return {
			...datas,
			numList: [
				{ name: '提交审批', desc: '提交审批' },
				{ name: '审批中' },
				{ name: '审批通过' },
			],
			current: 1,
			userInfoAudit: {},
			adminList: [],
			selectedItem: '请选择',
			borrowAdminId: '',
			// 归还审批影像
			imagePath: '',
			// 实际归还影像
			realityFilePath:'',
			stepOptions:[],
			
			renewAdminId:'',
			selectedRenew: '请选择',
			expectDateNew:'请选择日期',
			expectTimeNew:'请选择时间',
		}

	},
	computed: {},
	onShow() {
		that = this
		that.userInfoAudit = uni.getStorageSync("userInfoAudit")
		console.info(that.userInfoAudit)
		that.imagePath = that.userInfoAudit.restitutionFile
		that.realityFilePath = that.userInfoAudit.realityFile
		
		that.stepOptions = []
		const step = {title:'借用申请',desc:'申请时间：'+that.userInfoAudit.borrowApplyTime}
		that.stepOptions.push(step)
		if(that.userInfoAudit.orderStatus == 0){
			const step = {title:'借用待审批',desc:'审批人：'+that.userInfoAudit.borrowAdminName}
			that.stepOptions.push(step)
		}
		if(that.userInfoAudit.orderStatus == 11){
			const step = {title:'取消申请',desc:' '}
			that.stepOptions.push(step)
		}
		if(that.userInfoAudit.orderStatus == 1){
			const step = {title:'借用审批驳回',desc:'理由：'+that.userInfoAudit.borrowReject}
			that.stepOptions.push(step)
		}else{
			if(that.userInfoAudit.borrowAdminTime){
				const step = {title:'借用审批通过',desc:'审批人：'+that.userInfoAudit.borrowAdminName+' ， 审批时间：'+that.userInfoAudit.borrowAdminTime}
				that.stepOptions.push(step)
			}
		}
		if(that.userInfoAudit.orderStatus > 2 && that.userInfoAudit.orderStatus < 11){
			const step = {title:'仪器借用',desc:'借用时间：'+that.userInfoAudit.borrowTime}
			that.stepOptions.push(step)
		}
		
		if(that.userInfoAudit.orderStatus >= 5 && that.userInfoAudit.orderStatus < 11){
			const step = {title:'归还申请',desc:'申请时间：'+that.userInfoAudit.restitutionApplyTime}
			that.stepOptions.push(step)
		}
		if(that.userInfoAudit.orderStatus == 6){
			const step = {title:'归还审批驳回',desc:'理由：'+that.userInfoAudit.restitutionReject}
			that.stepOptions.push(step)
		}else if(that.userInfoAudit.orderStatus > 6 && that.userInfoAudit.orderStatus < 11){
			if(that.userInfoAudit.restitutionAdminTime){
				const step = {title:'归还审批通过',desc:'审批人：'+that.userInfoAudit.restitutionAdminName+' ， 审批时间：'+that.userInfoAudit.restitutionAdminTime}
				that.stepOptions.push(step)
			}
		}
		if(that.userInfoAudit.orderStatus > 7 && that.userInfoAudit.orderStatus < 11){
			const step = {title:'仪器归还',desc:'归还时间：'+that.userInfoAudit.realityTime}
			that.stepOptions.push(step)
		}
		
		
		
		uni.request({
			url: '/api/h5/getAdminList',
			method: "POST",
			header: {},
			data: {},
			success: (res) => {
				that.adminList = res.data.data
			},
			fail: (err) => {
				console.error(err);
			}
		});
	},
	created() {
		that = this
	},
	onReady() {
	},
	onLoad(options) {
		that = this
		console.log(options)
	},
	methods: {
		// 申请归还弹出框
		openReturnDialog(){
			that.imagePath = ''
			that.$refs.retyrnPopup.open()
		},
		// 申请延期归还弹出框
		openDelayDialog(){
			that.$refs.popup.open()
		},
		
		onDateChange: function(e) {
			this.expectDateNew = e.detail.value
		},
		onDateTimeChange: function(e) {
			this.expectTimeNew = e.detail.value
		},
		
		changeMenu(e) {
			that.current = e
		},
		gotoInfo(item) {
			that.$common.navigateTo('/pages/audit/details/index')
		},
		
		onPickerChange(e) {
			this.selectedItem = this.adminList[e.detail.value]['userName']
			this.borrowAdminId = this.adminList[e.detail.value]['id']
		},
		onRenewPickerChange(e){
			this.selectedRenew = this.adminList[e.detail.value]['userName']
			this.renewAdminId = this.adminList[e.detail.value]['id']
		},
		
		submitInfo() {
			if(that.borrowAdminId == ''){
				uni.showToast({
					title: '请选择归还审批人',//标题 必填
					icon: 'none',
				})
				return;
			}
			if(that.imagePath == ''){
				uni.showToast({
					title: '请先上传影像',//标题 必填
					icon: 'none',
				})
				return;
			}
			
			let formData = {
				orderId: this.userInfoAudit.id,
				restitutionAdminId: this.borrowAdminId,
				restitutionFile: this.imagePath,
				userId: uni.getStorageSync('userId'),
			}
			uni.request({
				url: '/api/h5/deviceRestitutionApply',
				method: "POST",
				header: {},
				data: formData,
				success: (res) => {
					if (res.data.code == 200) {
						that.$refs.retyrnPopup.close()
						
						uni.showToast({
							title: '仪器归还申请成功',//标题 必填
							icon: 'none',//图标
						    mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: '1500',//提示的延迟时间，单位毫秒，默认：1500
						    success: '',//接口调用成功的回调函数	
						    fail: '',//接口调用失败的回调函数	
						    complete: function(res){},//接口调用结束的回调函数（调用成功、失败都会执行）
						});
						setTimeout(()=>{
							uni.navigateBack()
						},1500)
			
					} else {
						
						uni.showToast({
							title: '系统异常，请联系管理员',//标题 必填
							icon: 'none',//图标
						    mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: '1000',//提示的延迟时间，单位毫秒，默认：1500
						    success: '',//接口调用成功的回调函数	
						    fail: '',//接口调用失败的回调函数	
						    complete: function(res){
							},//接口调用结束的回调函数（调用成功、失败都会执行）
						});
						
					}
				},
				fail: (err) => {
					console.error(err);
				}
			});
		},
		uploadRealityFile(file) {
			let _this = this
			uni.uploadFile({
				url: '/api/h5/upload',
				filePath: file.tempFilePaths[0] || '',
				name: 'file',
				formData: {},
				headers: { 'Content-Type': 'multipart/form-data' },
				success: (uploadFileRes) => {
					console.log(uploadFileRes)
					let jsonDes = JSON.parse(uploadFileRes.data)
					console.log(jsonDes)
					if (jsonDes && jsonDes.code == 200) {
						_this.realityFilePath = jsonDes.data
						
						let formData = {
							orderId: _this.userInfoAudit.id,
							realityFile : _this.realityFilePath,
							userId: uni.getStorageSync('userId'),
						}
						uni.request({
							url: '/api/h5/realityFileUpload',
							method: "POST",
							header: {},
							data: formData,
							success: (res) => {
								
							}
						})
						
						
					} else {
						uni.showToast({
							title: '上传失败',
							icon: 'success',
							duration: 1500
						});
					}
				},
				fail: (err) => {
					uni.showToast({
						title: '图片上传接口调用失败',
						icon: 'success',
						duration: 1500
					});
				}
			})
			
		},
		
		chooseImages(type) {
			let _this = this
			uni.chooseImage({
				count: 1, // 默认9
				sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
				success: (res) => {
					if(type == 1){
						_this.uploadImages(res)
					}else{
						_this.uploadRealityFile(res)
					}
					
				}
			});
		},
		uploadImages(file) {
			let _this = this
			uni.uploadFile({
				url: '/api/h5/upload',
				filePath: file.tempFilePaths[0] || '',
				name: 'file',
				formData: {},
				headers: { 'Content-Type': 'multipart/form-data' },
				success: (uploadFileRes) => {
					console.log(uploadFileRes)
					let jsonDes = JSON.parse(uploadFileRes.data)
					console.log(jsonDes)
					if (jsonDes && jsonDes.code == 200) {
						_this.imagePath = jsonDes.data
					} else {
						uni.showToast({
							title: '上传失败',
							icon: 'success',
							duration: 2000
						});
					}
				},
				fail: (err) => {
					uni.showToast({
						title: '图片上传接口调用失败',
						icon: 'success',
						duration: 2000
					});
				}
			})
		},
		
		// 延期归还申请
		submitDelayInfo() {
			if(that.renewAdminId == ''){
				uni.showToast({
					title: '请选择审批人',
					icon: 'none',
				})
				return;
			}
			if(that.expectDateNew == '请选择日期' || that.expectTimeNew == '请选择时间'){
				uni.showToast({
					title: '请选择预计归还时间',
					icon: 'none',
				})
				return;
			}
			
			let formData = {
				orderId: that.userInfoAudit.id,
				renewAdminId: that.renewAdminId,
				renewExpectTime: that.expectDateNew + ' ' + that.expectTimeNew,
			}
			uni.request({
				url: '/api/h5/deviceRenewApply',
				method: "POST",
				header: {},
				data: formData,
				success: (res) => {
					if (res.data.code == 200) {
						that.$refs.popup.close()
						uni.showToast({
							title: '仪器延期归还申请成功',//标题 必填
							icon: 'none',//图标
							mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
							duration: '1500',//提示的延迟时间，单位毫秒，默认：1500
							success: '',//接口调用成功的回调函数	
							fail: '',//接口调用失败的回调函数	
							complete: function(res){},//接口调用结束的回调函数（调用成功、失败都会执行）
						});
						setTimeout(()=>{
							uni.navigateBack()
						},1500)
						
					} else {
						uni.showToast({
							title: '系统异常，请联系管理员',//标题 必填
							icon: 'none',//图标
							mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false
							duration: '1500',//提示的延迟时间，单位毫秒，默认：1500
							success: '',//接口调用成功的回调函数	
							fail: '',//接口调用失败的回调函数	
							complete: function(res){},//接口调用结束的回调函数（调用成功、失败都会执行）
						});
						
					}
				},
				fail: (err) => {
					console.error(err);
				}
			});
		},
		
	}
}